import type { VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { EdubaseMajorsApi } from '#/api/edu/base/setting/majors';
import type { DescriptionItemSchema } from '#/components/description';

import { h } from 'vue';

import { DICT_TYPE } from '@vben/constants';
import { getDictOptions } from '@vben/hooks';
import { handleTree } from '@vben/utils';

import { Typography } from 'ant-design-vue';

import { getMajorsList } from '#/api/edu/base/setting/majors';
import { getRangePickerDefaultProps } from '#/utils';

const { Paragraph } = Typography;
/** 新增/修改的表单 */
export function useFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'id',
      component: 'Input',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'parentId',
      label: '上级专业管理',
      component: 'ApiTreeSelect',
      componentProps: {
        allowClear: true,
        api: async () => {
          const data = await getMajorsList({});
          data.unshift({
            id: 0,
            majorsName: '顶级专业管理',
          });
          return handleTree(data);
        },
        labelField: 'majorsName',
        valueField: 'id',
        childrenField: 'children',
        placeholder: '请选择上级专业管理',
        treeDefaultExpandAll: true,
      },
      rules: 'selectRequired',
    },
    {
      fieldName: 'majorsCode',
      label: '专业编码',
      rules: 'required',
      component: 'Input',
      componentProps: {
        placeholder: '请输入专业编码',
      },
    },
    {
      fieldName: 'majorsName',
      label: '专业名称',
      rules: 'required',
      component: 'Input',
      componentProps: {
        placeholder: '请输入专业名称',
      },
    },
    {
      fieldName: 'degreeAwardedCode',
      label: '学位授予门类编码',
      component: 'Input',
      componentProps: {
        placeholder: '请输入学位授予门类编码',
      },
    },
    {
      fieldName: 'degreeAwardedName',
      label: '学位授予门类名称',
      component: 'Input',
      componentProps: {
        placeholder: '请输入学位授予门类名称',
      },
    },
    {
      fieldName: 'durationStudy',
      label: '修业年限',
      component: 'Input',
      componentProps: {
        placeholder: '请输入修业年限',
      },
    },
    {
      fieldName: 'setYear',
      label: '设置年份',
      component: 'DatePicker',
      componentProps: {
        picker: 'year',
        format: 'YYYY',
        valueFormat: 'YYYY',
      },
    },
    {
      fieldName: 'period',
      label: '学段',
      rules: 'required',
      component: 'Select',
      componentProps: {
        options: getDictOptions(DICT_TYPE.EDU_PERIOD, 'number'),
        placeholder: '请选择学段',
      },
    },
    {
      fieldName: 'status',
      label: '是否有效',
      rules: 'required',
      component: 'Select',
      defaultValue: 1,
      componentProps: {
        options: getDictOptions(DICT_TYPE.COMMON_STATUS, 'number'),
        placeholder: '请选择是否有效',
      },
    },
  ];
}

/** 详情页字段 */
export function useDetailSchema(): DescriptionItemSchema[] {
  return [
    {
      label: '专业编码',
      field: 'majorsCode',
    },
    {
      label: '专业名称',
      field: 'majorsName',
    },
    {
      label: '学位授予门类编码',
      field: 'degreeAwardedCode',
    },
    {
      label: '学位授予门类名称',
      field: 'degreeAwardedName',
    },
    {
      label: '上级编码',
      field: 'parentCode',
    },
    {
      label: '一级学科（门类）编码',
      field: 'firstLevelCode',
    },
    {
      label: '一级学科（门类）名称',
      field: 'firstLevelName',
    },
    {
      label: '二级学科（专业类）编码',
      field: 'secondLevelCode',
    },
    {
      label: '二级学科（专业类）名称',
      field: 'secondLevelName',
    },
    {
      label: '修业年限',
      field: 'durationStudy',
    },
    {
      label: '设置年份',
      field: 'setYear',
    },
    {
      label: '学段编码',
      field: 'period',
      render: (val) =>
        h(DictTag, {
          type: DICT_TYPE.EDU_PERIOD,

          value: val,
        }),
    },
    {
      label: 'id所有路径',
      field: 'idPath',
    },
    {
      label: '编码所有路径',
      field: 'codePath',
    },
    {
      label: '是否有效',
      field: 'status',
      render: (val) =>
        h(DictTag, {
          type: DICT_TYPE.COMMON_STATUS,
          value: val,
        }),
    },
  ];
}

/** 列表的搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'majorsCode',
      label: '专业编码',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入专业编码',
      },
    },
    {
      fieldName: 'majorsName',
      label: '专业名称',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入专业名称',
      },
    },
    {
      fieldName: 'degreeAwardedCode',
      label: '学位授予门类编码',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入学位授予门类编码',
      },
    },
    {
      fieldName: 'degreeAwardedName',
      label: '学位授予门类名称',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入学位授予门类名称',
      },
    },
    {
      fieldName: 'parentCode',
      label: '上级编码',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入上级编码',
      },
    },
    {
      fieldName: 'firstLevelCode',
      label: '一级学科（门类）编码',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入一级学科（门类）编码',
      },
    },
    {
      fieldName: 'firstLevelName',
      label: '一级学科（门类）名称',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入一级学科（门类）名称',
      },
    },
    {
      fieldName: 'secondLevelCode',
      label: '二级学科（专业类）编码',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入二级学科（专业类）编码',
      },
    },
    {
      fieldName: 'secondLevelName',
      label: '二级学科（专业类）名称',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入二级学科（专业类）名称',
      },
    },
    {
      fieldName: 'setYear',
      label: '设置年份',
      component: 'DatePicker',
      componentProps: {
        picker: 'year',
        format: 'YYYY',
        valueFormat: 'YYYY',
      },
    },
    {
      fieldName: 'period',
      label: '学段',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: getDictOptions(DICT_TYPE.EDU_PERIOD, 'number'),
        placeholder: '请选择学段',
      },
    },
    {
      fieldName: 'status',
      label: '是否有效',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: getDictOptions(DICT_TYPE.COMMON_STATUS, 'number'),
        placeholder: '请选择是否有效',
      },
    },
    {
      fieldName: 'createTime',
      label: '创建时间',
      component: 'RangePicker',
      componentProps: {
        ...getRangePickerDefaultProps(),
        allowClear: true,
      },
    },
  ];
}

/** 列表的字段 */
export function useGridColumns(): VxeTableGridOptions<EdubaseMajorsApi.Majors>['columns'] {
  return [
    {
      field: 'id',
      title: '主键',
      minWidth: 120,
      visible: false,
    },
    {
      field: 'majorsName',
      title: '专业名称',
      minWidth: 200,
      treeNode: true,
    },
    {
      field: 'majorsCode',
      title: '专业编码',
      minWidth: 120,
    },
    {
      field: 'degreeAwardedCode',
      title: '学位授予门类编码',
      minWidth: 160,
    },
    {
      field: 'degreeAwardedName',
      title: '学位授予门类名称',
      minWidth: 160,
    },
    {
      field: 'rate',
      title: '级别',
      minWidth: 120,
      visible: false,
    },
    {
      field: 'parentId',
      title: '上级id',
      minWidth: 120,
      visible: false,
    },
    {
      field: 'firstLevelCode',
      title: '一级学科（门类）编码',
      minWidth: 120,
    },
    {
      field: 'firstLevelName',
      title: '一级学科（门类）名称',
      minWidth: 200,
    },
    {
      field: 'secondLevelCode',
      title: '二级学科（专业类）编码',
      minWidth: 120,
    },
    {
      field: 'secondLevelName',
      title: '二级学科（专业类）名称',
      minWidth: 200,
    },
    {
      field: 'durationStudy',
      title: '修业年限',
      minWidth: 120,
    },
    {
      field: 'setYear',
      title: '设置年份',
      minWidth: 120,
    },
    {
      field: 'period',
      title: '学段',
      minWidth: 120,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.EDU_PERIOD },
      },
    },
    {
      field: 'status',
      title: '是否有效',
      minWidth: 120,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.COMMON_STATUS },
      },
    },
    {
      field: 'parentCode',
      title: '上级编码',
      minWidth: 120,
    },

    {
      field: 'idPath',
      title: 'id所有路径',
      minWidth: 120,
    },
    {
      field: 'codePath',
      title: '编码所有路径',
      minWidth: 120,
    },
    {
      field: 'createTime',
      title: '创建时间',
      minWidth: 120,
      formatter: 'formatDateTime',
    },
    {
      field: 'actions',
      title: '操作',
      width: 280,
      fixed: 'right',
      slots: { default: 'actions' },
    },
  ];
}
